package com.project.controller;

import com.common.annotation.Anonymous;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.project.domain.dto.projectCheckDto.*;
import com.project.domain.entity.BizReviewPlan;
import com.project.service.IProjectApplyService;
import com.project.service.IProjectCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * Created by cw13031 on 2025/2/6.
 * Description:
 */
@RestController
@RequestMapping("/project/check")
public class ProjectCheckController extends BaseController{

    @Autowired
    IProjectCheckService projectCheckService;

    @Autowired
    IProjectApplyService projectApplyService;

    @PostMapping(value = "/search")
    public TableDataInfo search(@RequestBody SearchReqDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), dto.getOrderBy());
        List<HashMap<String, Object>> list =projectCheckService.search(dto);
        return getDataTable(list);
    }

    @PostMapping(value = "/search-statistics")
    public AjaxResult searchStatistics(@RequestBody SearchReqDto dto)
    {
        List<HashMap<String, Integer>> list =projectCheckService.searchStatistics(dto);
        return success(list);
    }



    @PostMapping(value = "/lixiang-action")
    public AjaxResult lixiangAction(@RequestBody LixiangActionReqDto dto)
    {
        int res =projectCheckService.changeProjectState(dto);
        return success();
    }

    @GetMapping(value = "/history")
    public AjaxResult checkHistory(@RequestParam("bookId") String bookId)
    {
        return success(projectCheckService.getCheckHistoryByBookId(bookId));
    }

    /**
     * 转入评审
     * 当计划处于结束且BIZ_REVIEW_PLAN没有该计划信息时，显示“转入评审”按钮
     * @param plan 计划id，即infoId
     * @return 执行结果
     */
    @PostMapping(value = "/review")
    public AjaxResult review(@RequestBody BizReviewPlan plan)
    {
        projectCheckService.review(plan);
        return success();
    }

    /**
     * 通过评审计划id获取评审计划完整信息
     * @param planId 评审计划id
     * @return 评审计划完整信息
     */
    @GetMapping("/review")
    public AjaxResult review(@RequestParam("planId") String planId) {
        Map<String, Object> reviewPlan = projectCheckService.getReviewPlanById(planId);
        if (reviewPlan == null) {
            return success(Collections.emptyMap());
        }

        String fileId = Optional.ofNullable(reviewPlan.get("fileId"))
                .map(Object::toString)
                .orElse("");

        if (StringUtils.hasText(fileId)) {
            String[] fileIds = fileId.split(",");
            List<HashMap<String, String>> fileInfo = projectApplyService.getFileInfoByFileIds(fileIds);
            reviewPlan.put("fileInfo", fileInfo);
        }

        return success(reviewPlan);
    }

    /**
     * 管理员查看所有的通知
     * @return x
     */
    @GetMapping("/msg-list")
    public TableDataInfo msgList(MsgListReqDto dto) {
        startPage();
        ArrayList<MsgListResDto> res = projectCheckService.msgList(dto);
        return getDataTable(res);
    }

    @GetMapping("/msg-detail")
    public AjaxResult msgDetail(@ModelAttribute MsgDetailReqDto dto) {
        MsgDetailResDto res = projectCheckService.msgDetail(dto);
        return success(res);
    }

}
